package com.flute.icrawler.app.util;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.lobobrowser.html.parser.DocumentBuilderImpl;
import org.lobobrowser.html.parser.InputSourceImpl;
import org.lobobrowser.html.test.SimpleUserAgentContext;
import org.xml.sax.SAXException;
import org.w3c.dom.Document;

public class DynamicParserDocment extends AbstractParserDocment {

	public DynamicParserDocment(String strUrl, String charset,
			InputStream sourceInputStream) {
		super(strUrl, charset, sourceInputStream);
	}

	public DynamicParserDocment(String url, String charset, byte[] content) {
		super(url, charset, content);
	}

	/**
	 * Transform html and js to HTML DOM
	 * 
	 * @return Document
	 */
	protected Document getDocument() throws ParserException {
		// close cobra's log
		Logger.getLogger("org.lobobrowser").setLevel(Level.OFF);

		try {
			SimpleUserAgentContext context = new SimpleUserAgentContext();
			context.setExternalCSSEnabled(false);
			context.setScriptingEnabled(true);

			DocumentBuilderImpl dbi = new DocumentBuilderImpl(context);

			return dbi.parse(new InputSourceImpl(sourceInputStream, sourceName,
					charset));

		} catch (MalformedURLException e) {

			throw new ParserException("connect url error" + e.getMessage());
		} catch (SAXException e) {

			throw new ParserException("parser html document error"
					+ e.getMessage());
		} catch (IOException e) {

			throw new ParserException("Read html source IO error"
					+ e.getMessage());
		} finally {
			try {
				sourceInputStream.close();
			} catch (Exception e) {

			}
		}
	}

}
